<?php

namespace App\Http\Controllers;

use App\Models\AddressModel;
use App\Models\HouseList;
use App\Models\HouseMoldingModel;
use App\Services\ElasticSearchService;
use App\Services\HouseTypeInfoServer;
use App\Services\HouseWayPayHouse;
use App\Services\HouseWays;
use App\Services\HouseWaySecondHouse;
use App\Tools\JsonTools;
use App\Tools\RedisTools;
use Illuminate\Http\Request;

class HouserListController extends Controller
{
    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/14,
     * @Time: 15:29,
     * 房源列表
     */
    public function housePushList(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 5);
        $offset = intval(($page-1))*intval($limit);
        $data=$request->all();
        $houseWay=$data['houseWay'];
        if($houseWay==1){
            $house = new HouseWayPayHouse($data,$limit,$offset,$houseWay);
        }else{
            $house = new HouseWaySecondHouse($data,$limit,$offset,$houseWay);
        }
        $config = (new HouseWays($house))->doWork();
        if($config){
            return JsonTools::JsonSuccess($config,200,'msg');
        }else{
            return JsonTools::JsonError([],10001,'没有数据');
        }


    }

    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/14,
     * @Time: 16:19,
     * 选择城市
     */
    public function houseCity()
    {
        $redis = (RedisTools::getInstance())->connectRedis();
        $redisInfo = $redis->get('cityAddress');
        if ($redisInfo) {
            $redisInfo = json_decode($redisInfo, true);
        } else {
            $redisInfo = AddressModel::where('name', 'like', '%市%')->get('name')->toArray();
            $redis->set('cityAddress', json_encode($redisInfo, 256), 3600);
        }
        return JsonTools::JsonSuccess($redisInfo, 200, 'ok');
    }

    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/15,
     * @Time: 15:43,
     * 选择区域区域
     */
    public function houseAreaInfo(Request $request)
    {
        $redis = (RedisTools::getInstance())->connectRedis();
        $city = $request->get('city');
        $redisInfo = $redis->get('areaAddress' . $city);
        if ($redisInfo) {
            $redisInfo = json_decode($redisInfo, true);
        } else {
            $cityInfo = AddressModel::where('name', $city)->first()->toArray();
            $redisInfo = AddressModel::where('pid', $cityInfo['id'])->get()->toArray();
            $redis->set('areaAddress' . $city, json_encode($redisInfo, 256), 3600);
        }
        return JsonTools::JsonSuccess($redisInfo, 200, 'ok');
    }

    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/15,
     * @Time: 19:14,
     * 选择户型
     */
    public function houseMolding()
    {
        $houseMolding = HouseMoldingModel::get()->toArray();
        return JsonTools::JsonSuccess($houseMolding, 200, 'ok');
    }


    public function esHouseList(){
        $res=ElasticSearchService::getInstance();
        $res->createIndex('house_list','house_name');
    }
}
